package logic

import (
	"context"
	"errors"
	"taobao-merchandise/mysql"

	"taobao-merchandise/taobaorpc/internal/svc"
	"taobao-merchandise/taobaorpc/taobaorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品添加
func (l *GoodsAddLogic) GoodsAdd(in *taobaorpc.GoodsAddRequest) (*taobaorpc.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	if err := user.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}

	var goods = mysql.Goods{
		Title:  in.Title,
		Price:  in.Price,
		Image:  in.Image,
		UserId: in.UserId,
	}

	err := goods.GoodsAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("商品添加失败")
	}

	return &taobaorpc.GoodsAddResponse{
		GoodsId: int64(goods.ID),
	}, nil
}
